package com.qf.servlet;

import com.qf.domain.Book;
import com.qf.service.BookService;
import com.qf.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * projectName:day13_booksystem
 * author:liuXin
 * time:2022/9/29 11:11
 * description:
 */
@WebServlet("/addBook")
@MultipartConfig(maxFileSize = 1024*1024*5,maxRequestSize = 1024*1024*20)
public class AddBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String price = request.getParameter("price");

        try {
            BookService bookService=new BookServiceImpl();
            Book book=new Book(0,title,author,new BigDecimal(price));
            bookService.addBook(book);
            response.sendRedirect("booklistpage");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","添加失败"+e.getMessage());
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
